package com.example.demo.util.word;


import java.io.*;

import org.apache.poi.hslf.blip.PNG;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.poifs.filesystem.FileMagic;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

/**
 * fileName:DocUtil
 * description:
 * author: LJV
 * createTime:2022/4/22 10:49
 * version:1.0.0
 */
public class DocUtil {

    /**
     * 读取doc文件内容
     *
     * @param fs
     *            想要读取的文件对象
     * @return 返回文件内容
     * @throws IOException
     */
    public static String doc2String(FileInputStream fs) throws IOException {
        StringBuilder result = new StringBuilder();
        WordExtractor re = new WordExtractor(fs);
        result.append(re.getText());
        re.close();
        return result.toString();
    }

    public static String doc2String(File file) throws IOException {
        return doc2String(new FileInputStream(file));
    }

    static String read(InputStream is) throws Exception {
        System.out.println(FileMagic.valueOf(is));
        String text = "";
        if (FileMagic.valueOf(is) == FileMagic.OLE2) {
            WordExtractor ex = new WordExtractor(is);
            text = ex.getText();
            ex.close();
        } else if (FileMagic.valueOf(is) == FileMagic.OOXML) {
            XWPFDocument doc = new XWPFDocument(is);
            XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
            text = extractor.getText();
            extractor.close();
        }else if(FileMagic.valueOf(is) == FileMagic.JPEG){
            System.out.println("\"图片\" = " + "图片");
        }
        return text;
    }

    public static String readWord(String filePath){
        InputStream is=null;
        String read=null;
        try {
            is = new BufferedInputStream(new FileInputStream("D:\\\\Huading\\\\高德地图\\\\高德模拟(1).docx")); //really a OOXML Word file
            read = read(is);
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return read;
    }

    public static void main(String[] args) {
        /*File file = new File("D:\\Huading\\高德地图\\高德模拟(1).docx");
        try {
            System.out.println(doc2String(file));
        } catch (IOException e) {
            e.printStackTrace();
        }*/
        InputStream is=null;
        try {
//            is = new BufferedInputStream(new FileInputStream("D:\\\\Huading\\\\高德地图\\\\高德模拟(1).docx")); //really a OOXML Word file
            is = new BufferedInputStream(new FileInputStream("D:\\img\\1.png")); //really a OOXML Word file
            System.out.println(read(is));
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}
